﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text.RegularExpressions;

namespace GSBA.Common
{
    /// <summary>
    /// Utility class for reading and writing objects to CSV files.
    /// </summary>
    public static class CsvLib
    {
        // Matches individual values in a CSV row where those values may be enclosed in "double quotes"
        private static readonly Regex csvRowExpression = new Regex(@"^((""(?<value>[^""]*)""|(?<value>[^""\,]*)),)*(""(?<value>[^""]*)""|(?<value>[^""\,]*))$");

        public static string[] ParseCsvRow(string row)
        {
            var rowRegexMatch = csvRowExpression.Match(row);
            var captures = rowRegexMatch.Groups["value"].Captures.OfType<Capture>();

            return captures.Select(c => c.Value).ToArray();
        }

    }
}
